using System;
using System.Collections.Generic;
using TTXNA.src.Engine;
using TTXNA.src.Engine.Terrain;
using TTXNA.src.Engine.Companies;
using TTXNA.src.Engine.Towns;
using TTXNA.src.Engine.Vehicles;
using TTXNA.src.Util;
using TTXNA.src.Engine.Graphics;
using Microsoft.Xna.Framework;
using TTXNA.src.Engine.Create.Tools;

namespace TTXNA.src.Engine.World
{
    public class GameWorld : TTUpdateable, TTRenderable
    {
        private TTXNAGame game;

        private const float timeFactor = 86400 * 0.5f;
        private bool gamePaused;

        private Map map;
        
        //public static DDDRoadNetwork roadNetwork;

        //private GameTimer frameTimer;
        //private GameTimer gameTimer;

        private int frames;
        private int lastFrames;

        private DateTime worldDate;
        
        private Dictionary<int, Company> gameCompanies;
        private List<Town> gameTowns;

        private TimeSpan perSecond;

        public GameWorld(TTXNAGame game, int width, int length)
        {
            this.game = game;
            this.map = new Map(width, length);
            gameCompanies = new Dictionary<int, Company>();
            gameTowns = new List<Town>();
            //roadNetwork = new DDDRoadNetwork();
            
            worldDate = new DateTime(1950, 1, 1);

            SetupWorldMap();
        }

        public void AddVehicle(int playerNumber, Vehicle vehicle)
        {
            gameCompanies[playerNumber].AddVehicle(vehicle);
        }

        public void CreateNewTown(string name, Coordinate coord)
        {
            gameTowns.Add(new Town(name, coord));
        }

        public int CreateNewCompany()
        {
            int companyNum = 0;

            while (gameCompanies.ContainsKey(companyNum))
            {
                companyNum++;
            }

            gameCompanies[companyNum] = new Company(new DateTime(worldDate.Year));
            return companyNum;
        }

        private void SetupWorldMap()
        {
            for (int y = 0; y < map.Length; y++)
            {
                for (int x = 0; x < map.Width; x++)
                {
                    DDDCoordinate coord = new DDDCoordinate(x, 0, y);
                    map.SetTile(new Tile(coord, LandType.Grass));
                }
            }
        }

        public void Update(GameTime elapsedTime)
        {
            if (!Paused)
            {
                TimeSpan passed = new TimeSpan(0, 0, (int)(elapsedTime.ElapsedGameTime.TotalSeconds * timeFactor));

                foreach (Company c in gameCompanies.Values)
                {
                    c.Update(passed);
                }              

                foreach (Town t in gameTowns)
                {
                    t.Update(passed);
                }

                map.Update(elapsedTime);

                worldDate = worldDate.AddSeconds(elapsedTime.ElapsedGameTime.TotalSeconds * timeFactor);
            }
        }

        public Company GetCompany(int num)
        {
            return gameCompanies[num];
        }

        public Map Map
        {
            get { return map; }
        }

        public bool Paused
        {
            get { return gamePaused; }
            set { gamePaused = value; }
        }

        public Dictionary<int, Company> Companies
        {
            get { return gameCompanies; }
        }

        //public TTScene Scene
        //{
        //    get { return scene; }
        //}

        //public DDDRoadNetwork RoadNetwork
        //{
        //    get { return roadNetwork; }
        //}

        public DateTime CurrentDate
        {
            get { return worldDate; }
        }

        public List<Town> Towns
        {
            get { return gameTowns; }
        }

        public int Width
        {
            get { return map.Width; }
        }

        public int Length
        {
            get { return map.Length; }
        }

        #region TTRenderable Members

        public void Render(TTCamera camera)
        {
            
        }

        #endregion
    }
}
